package com.example.home.youthstation;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

import static com.example.home.tool.StaticActi.ip;

public class YouthStationActivity extends AppCompatActivity {

    private ImageView imageVfiew;
    private ImageView back;
    private TextView tv_introduce;
    private Handler handler;
    private Gson gson;
    private YouthBean youthBean;
    private RecyclerView cen_yizhan;
    private RecyclerView botYIzhan;
    private YouthFristBean youthFristBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youth_station);
        
        initView();
        //页面图片和介绍
        getHomePage();
        //中间三
        getcentBean();
        //下部列表
        getbotYIzhanBean();
    }

    private void getbotYIzhanBean() {
        OKHttpTool.doNetRequest("prod-api/api/youth-inn/youth-inn/list",
                OKHttpTool.GET,
                BotYiZhanBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        BotYiZhanBean botYiZhanBean = (BotYiZhanBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                binyizhan(botYiZhanBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {
                        Log.v("xhf","访问失败");
                    }
                }
        );
    }

    private void binyizhan(BotYiZhanBean botYiZhanBean) {
        BotYIzhanAdapter botYIzhanAdapter = new BotYIzhanAdapter(this,botYiZhanBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        botYIzhan.setLayoutManager(linearLayoutManager);
        botYIzhan.setAdapter(botYIzhanAdapter);
    }


    private void getHomePage() {
        OKHttpTool.doNetRequest("prod-api/api/youth-inn/talent-policy-area/7",
                OKHttpTool.GET,
                YouthFristBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        YouthFristBean youthFristBean = (YouthFristBean) obj;
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Glide.with(YouthStationActivity.this).load(ip+youthFristBean.getData().getImgUrl()).into(imageVfiew);
                                    String introduce = youthFristBean.getData().getIntroduce();
                                    if (introduce != null && !introduce.isEmpty()) {
                                        int maxLength = Math.min(50, introduce.length()); // 截取的最大长度，这里设定为50个字符
                                        String extractedText = introduce.substring(0, maxLength); // 截取前50个字符
                                        if (extractedText.length() < introduce.length()) {
                                            // 如果截取后的文本长度小于原始文本长度，说明需要添加 "..." 结尾
                                            extractedText += "...";
                                        }
                                        tv_introduce.setText("      "+extractedText);
                                    } else {
                                        // 内容为空或者为null时的处理
                                        tv_introduce.setText("内容为空");
                                    };
                                }
                            });
                        }

                    @Override
                    public void onFailure(String msg) {
                        Log.v("xhf","访问失败");
                    }
                }
        );
    }
    private void getcentBean() {
        OKHttpTool.doNetRequest("prod-api/api/youth-inn/talent-policy-area/list",
                OKHttpTool.GET,
                YouthBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        YouthBean youthBean = (YouthBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                centYizhan(youthBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {
                        Log.v("xhf","访问失败");
                    }
                }
        );
    }

    private void centYizhan(YouthBean youthBean1) {
        YiZhanApter yiZhanApter = new YiZhanApter(this,youthBean1.getData());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
        cen_yizhan.setLayoutManager(gridLayoutManager);
        cen_yizhan.setAdapter(yiZhanApter);
    }

    private void initView() {
        back = findViewById(R.id.back);
        imageVfiew = findViewById(R.id.imageView);
        tv_introduce = findViewById(R.id.tv_introduce);
        cen_yizhan = findViewById(R.id.cen_yizhan);
        botYIzhan = findViewById(R.id.botYIzhan);
        handler = new Handler();
        gson = new Gson();
    }
}